﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class AB45MATERIALReport : BaseReport
    {
        #region Variable
        byte[] b;
        string strCompany = "", strExcelFile = "";
        #endregion
        public AB45MATERIALReport()
            : base()
        {
        }
        public AB45MATERIALReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            columns.Add("SFACTORY");            
            //columns.Add("ABPSTARTDAT");
            columns.Add("ORDERNO");
            columns.Add("ARTICLE");
            columns.Add("BSFBEZ");
            columns.Add("BQTY");
            columns.Add("ORDERMTR");
            columns.Add("WFACTORY");
            columns.Add("ABPENDDAT");
            columns.Add("ABPERLDAT");
            columns.Add("DIFF");
            columns.Add("ACTMTR");
            columns.Add("BATCHNO");
            columns.Add("ABKKDAUFTNR");
            columns.Add("GAART");
            columns.Add("GAVAR");
            columns.Add("DES");
            //columns.Add("GAFARBE");
            columns.Add("COLORDES");
            columns.Add("GQTY");
            columns.Add("ALLOKG");
            columns.Add("SOLLKG");
            columns.Add("ACTKG");
            columns.Add("SUPPLIER");
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["YABPOS"];
                foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                }

                HideNullRows();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows()
        {
            try
            {
                int HidedRows = 0;
                int rowstart = 1;
                int rowend = 1;
                int col1, col2, col3;

                var val1 = exAgent.GetCellValue(1, 1);

                rowstart = 3; rowend = 5000; col1 = 6; col2 = 7;
                for (int Rowi = rowstart; Rowi <= rowend; Rowi++)
                {
                    val1 = exAgent.GetRangeValue(Rowi, Rowi, col1, col2).Trim();
                    if ((string)val1 == "0")
                    {
                        HidedRows++;
                        exAgent.HideRows(Rowi, col1, Rowi, col2);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void WriteExcelReport(string fileType)
        {
            string filename = "";
            string sheetName = "";
            string Standard = "AB45MATERIAL";
            strExcelFile = @"HLT\HLTPlanningReport.xls";
            string filePath = @"C:\intex.w\Report\" + strExcelFile;
            exAgent.Open(filePath);
            exAgent.Company = strCompany;
            exAgent.strExcelFile = strExcelFile;
            //string filedate = DateTime.Now.Year.ToString();
            string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            filename = @"Planning\浆纱\跟踪表" + filedate + ".xls";
            sheetName =DateTime.Now.Hour.ToString()+"-"+DateTime.Now.Minute.ToString();
           // exAgent.DeleteWorkSheet();
            exAgent.CreateNewSheet(filename, Standard, sheetName);
            FillInExcelData(columns, dsSource, 3,1);
            base.WriteExcelReport(fileType);
        }
    }
}
